package com.xja.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xja.mapper.CommentMapper;
import com.xja.model.dto.CommentDTO;
import com.xja.model.entity.Article;
import com.xja.model.entity.Comment;
import com.xja.model.entity.CommentLike;
import com.xja.model.entity.User;
import com.xja.model.vo.resp.CommentVO;
import com.xja.service.IArticleService;
import com.xja.service.ICommentLikeService;
import com.xja.service.ICommentService;
import com.xja.service.IUserService;
import com.xja.utils.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 文章评论表 服务实现类
 * </p>
 *
 * @author caijg
 */
@Service
@RequiredArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    private final IUserService userService;
    private final IArticleService articleService;
    private final ICommentLikeService commentLikeService;

    @Override
    public List<CommentVO> getArticleComments(Long articleId) {
        // 1. 查询评论
        List<Comment> commentList = this.query()
                .eq("article_id", articleId)
                .orderByDesc("like_count")
                .list();

        // 2. 封装VO
        return commentList.stream()
                .map(comment -> {
                    User commentUser = userService.query()
                            .eq("id", comment.getUserId())
                            .one();
                    return CommentVO.builder()
                            .id(comment.getId())
                            .avatarUrl(commentUser.getAvatar())
                            .content(comment.getContent())
                            .createTime(comment.getCreateTime())
                            .likeCount(comment.getLikeCount())
                            .isLiked(commentLikeService.query()
                                    .eq("comment_id", comment.getId())
                                    .eq("user_id", UserContext.getUid())
                                    .exists())
                            .nickname(commentUser.getNickname())
                            .build();
                }).toList();
    }

    @Override
    public void submitComment(CommentDTO commentDTO) {
        // 1.获取当前用户id
        Long uid = UserContext.getUid();
        // 2.封装评论对象
        Comment comment = new Comment();
        comment.setArticleId(commentDTO.getArticleId());
        comment.setContent(commentDTO.getContent());
        comment.setCreateTime(LocalDateTime.now());
        comment.setUserId(uid);

        // 3.文章的评论数加1
        Article article = articleService.query()
                .eq("id", commentDTO.getArticleId())
                .one();
        articleService.updateById(article.setCommentCount(article.getCommentCount() + 1));

        // 4.保存评论
        this.save(comment);
    }

    @Override
    public Boolean likeComment(Long commentId) {
        // 1.获取当前用户id
        Long uid = UserContext.getUid();
        // 2.查询点赞记录
        CommentLike one = commentLikeService.query()
                .eq("comment_id", commentId)
                .eq("user_id", uid)
                .one();

        // 3.判断点赞记录是否存在
        if (one == null) {
            // 3.1 不存在，则新增点赞记录
            CommentLike commentLike = new CommentLike();
            commentLike.setCommentId(commentId);
            commentLike.setUserId(uid);
            commentLikeService.save(commentLike);
            // 3.2 评论点赞数加1
            this.update()
                    .setSql("like_count = like_count + 1")
                    .eq("id", commentId)
                    .update();
            return true;
        } else {
            // 3.3 存在，则删除点赞记录
            commentLikeService.removeById(one.getId());
            // 3.4 评论点赞数减1
            this.update()
                    .setSql("like_count = like_count - 1")
                    .eq("id", commentId)
                    .update();
            return false;
        }
    }
}